package com.centaline.androidsalesblog.fragments.collection;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;

import com.android.volley.VolleyError;
import com.centaline.androidsalesblog.activities.sale.RentDetailActivity;
import com.centaline.androidsalesblog.adapter.RentListAdapter;
import com.centaline.androidsalesblog.api.sale.PostListApi;
import com.centaline.androidsalesblog.api.usercenter.CollectInfoListApi;
import com.centaline.androidsalesblog.base.BaseCollectFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.constants.SourceType;
import com.centaline.androidsalesblog.events.MineInfoUpdate;
import com.centaline.androidsalesblog.model.databaseobject.PostDo;
import com.centaline.androidsalesblog.model.responseobject.PostListRo;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IResponseListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * 租房
 * Created by ruanzb on 2016/3/18.
 */
public class RentCollFragment extends BaseCollectFragment<PostDo> {

    private PostListApi postListApi;

    @Override
    public void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

    @Override
    protected void initView() {
        super.initView();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void initApi(CollectInfoListApi collectInfoListApi) {
        collectInfoListApi.setSourceType(SourceType.ZUFANG);
    }

    @Override
    protected void initOtherApi() {
        postListApi = new PostListApi(RentCollFragment.class, PostListRo.class, new IResponseListener<PostListRo>() {
            @Override
            public void onResponse(PostListRo postListRo) {
                stopRefresh(postListRo.getList());
            }

            @Override
            public void onErrorResponse(VolleyError error) {
                mRecyclerView.refreshError();
            }
        });
        postListApi.setPostType("r");
    }

    /**
     * 用户中心数据变化
     */
    @Subscribe
    public void onEventMainThread(MineInfoUpdate mineInfoUpdate) {
        int status = mineInfoUpdate.getStatus() / 10;
        if (status == 1) {
            if (mineInfoUpdate.getStatus() == 12) {
                mRecyclerView.startRefresh();
            }
        }
    }

    @Override
    protected void requestList(String ids) {
        postListApi.setPostIds(ids);
        postListApi.startRequest();
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        return new RentListAdapter(getActivity(), GlideLoad.init(this), list);
    }

    @Override
    protected void ItemClick(PostDo postDo) {
        if (postDo.isOnline()) {
            startActivity(new Intent(getContext(), RentDetailActivity.class)
                    .putExtra(ExtraConstant.PostId, postDo.getPostId()));
        } else {
            toast("该房源已停租");
        }
    }
}
